package com.dmcloud.zuul.filter.audi;

import org.apache.commons.lang.StringUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * Created by xiaodao
 * date: 2019/11/7
 * 审计日志 日志的过滤器 应该在日志的过滤器后面.授权的过滤器前面
 */

public class AudiLogFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
        String user =(String) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        if(StringUtils.isBlank((String) httpServletRequest.getAttribute("updateLog"))){

            System.out.println("1. add log for "+ user);
        }

        filterChain.doFilter(httpServletRequest,httpServletResponse);
        if(StringUtils.isBlank((String) httpServletRequest.getAttribute("updateLog"))){

            System.out.println("3. update log to success ");
        }
    }
}
